package com.leetcode;

/**
 * TODO
 *
 * @author lefly
 * @version 1.0
 * @classname No122
 * @description 122. 买卖股票的最佳时机 II
 * @date 2021/3/9 下午7:33
 */
public class No122 {
    public static void main(String[] args) {
        int[] arr = {7,1,5,3,6,4};
        int maxProfit = maxProfit(arr);
        System.out.println(maxProfit);
    }

    public static int maxProfit(int[] prices) {
        // 对于 [a, b, c, d]，如果有 a <= b <= c <= d ，那么最大收益为 d - a。而 d - a = (d - c) + (c - b) + (b - a) ，因此当访问到一个 prices[i] 且 prices[i] - prices[i-1] > 0，那么就把 prices[i] - prices[i-1] 添加到收益中。
        int profit = 0;
        for (int i = 1;i < prices.length;i++){
            if(prices[i] > prices[i - 1]){
               profit += prices[i] - prices[i - 1];
            }
        }
        return profit;

    }

}
